package com.whf.android.jar.net.rx.t;


import androidx.annotation.NonNull;

import com.blankj.utilcode.util.GsonUtils;
import com.whf.android.jar.base.delegate.IPageLoadListener;
import com.whf.android.jar.constants.BaseEntries;
import com.whf.android.jar.net.callback.IError;
import com.whf.android.jar.net.callback.ISuccess;
import com.whf.android.jar.tool.Observer;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.WeakHashMap;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;


/**
 * 单独的 网络请求
 *
 * @author gf
 * @version 1.0
 */
public class RxRetrofit extends BaseRxRetrofit {

    protected final IError iError;
    protected final IPageLoadListener iPageLoadListener;

    public RxRetrofit(IError iError) {
        this.iError = iError;
        this.iPageLoadListener = null;
    }

    public RxRetrofit(IError iError, IPageLoadListener iPageLoadListener) {
        this.iError = iError;
        this.iPageLoadListener = iPageLoadListener;
    }

    /**
     *
     */
    public void get(String url, WeakHashMap<String, Object> params, final ISuccess listener) {
        if (getRetrofit() == null) {
            return;
        }
        if (iPageLoadListener != null) {
            iPageLoadListener.showLoading();
        }
        RxService service = getRetrofit().create(RxService.class);
        setSubscribe(service.get(url, params), new Observer<BaseEntries<Object>>() {
            @Override
            public void onNext(BaseEntries<Object> entries) {
                if (entries != null) {
                    if (entries.isOk()) {
                        if (listener != null) {
                            listener.onSuccess(GsonUtils.toJson(entries.getData()));
                        }
                    } else {
                        if (iError != null) {
                            iError.onError(entries.getCode(), entries.getMessage());
                        }
                    }
                }
                if (iPageLoadListener != null) {
                    iPageLoadListener.hideLoading();
                }
            }

            @Override
            public void onError(int code, String error) {
                if (iError != null) {
                    iError.onError(code, error);
                }
                if (iPageLoadListener != null) {
                    iPageLoadListener.hideLoading();
                }
            }
        });
    }

    /**
     *
     */
    public void post(String url, WeakHashMap<String, Object> params,
                     final ISuccess listener) {
        post(null, url, params, listener);
    }

    /**
     *
     */
    public void post(WeakHashMap<String, Object> lang,
                     String url,
                     WeakHashMap<String, Object> params,
                     final ISuccess listener) {
        if (getRetrofit() == null) {
            return;
        }
        if (lang == null) {
            lang = new WeakHashMap<>();
        }
        if (iPageLoadListener != null) {
            iPageLoadListener.showLoading();
        }
        RxService service = getRetrofit().create(RxService.class);
        RequestBody mBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"),
                GsonUtils.toJson(params));
        setSubscribe(service.post(lang, url, mBody), new Observer<BaseEntries<Object>>() {
            @Override
            public void onNext(BaseEntries<Object> entries) {
                if (entries != null) {
                    if (entries.isOk()) {
                        if (listener != null) {
                            listener.onSuccess(GsonUtils.toJson(entries.getData()));
                        }
                    } else {
                        if (iError != null) {
                            iError.onError(entries.getCode(), entries.getMessage());
                        }
                    }
                }
                if (iPageLoadListener != null) {
                    iPageLoadListener.hideLoading();
                }
            }

            @Override
            public void onError(int code, String error) {
                if (iError != null) {
                    iError.onError(code, error);
                }
                if (iPageLoadListener != null) {
                    iPageLoadListener.hideLoading();
                }
            }
        });
    }

    /**
     *
     */
    public void put(WeakHashMap<String, Object> lang,
                    String url,
                    WeakHashMap<String, Object> params,
                    final ISuccess listener) {
        if (getRetrofit() == null) {
            return;
        }
        if (iPageLoadListener != null) {
            iPageLoadListener.showLoading();
        }
        RxService service = getRetrofit().create(RxService.class);
        setSubscribe(service.put(lang, url, params), new Observer<BaseEntries<Object>>() {
            @Override
            public void onNext(BaseEntries<Object> entries) {
                if (entries != null) {
                    if (entries.isOk()) {
                        if (listener != null) {
                            listener.onSuccess(GsonUtils.toJson(entries.getData()));
                        }
                    } else {
                        if (iError != null) {
                            iError.onError(entries.getCode(), entries.getMessage());
                        }
                    }
                }
                if (iPageLoadListener != null) {
                    iPageLoadListener.hideLoading();
                }
            }

            @Override
            public void onError(int code, String error) {
                if (iError != null) {
                    iError.onError(code, error);
                }
                if (iPageLoadListener != null) {
                    iPageLoadListener.hideLoading();
                }
            }
        });
    }


    /**
     *
     */
    public void putRaw(WeakHashMap<String, Object> lang,
                       String url,
                       WeakHashMap<String, Object> params,
                       final ISuccess listener) {
        if (getRetrofit() == null) {
            return;
        }
        if (iPageLoadListener != null) {
            iPageLoadListener.showLoading();
        }
        RxService service = getRetrofit().create(RxService.class);
        RequestBody mBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"),
                GsonUtils.toJson(params));
        setSubscribe(service.putRaw(lang, url, mBody), new Observer<BaseEntries<Object>>() {
            @Override
            public void onNext(BaseEntries<Object> entries) {
                if (entries != null) {
                    if (entries.isOk()) {
                        if (listener != null) {
                            listener.onSuccess(GsonUtils.toJson(entries.getData()));
                        }
                    } else {
                        if (iError != null) {
                            iError.onError(entries.getCode(), entries.getMessage());
                        }
                    }
                }
                if (iPageLoadListener != null) {
                    iPageLoadListener.hideLoading();
                }
            }

            @Override
            public void onError(int code, String error) {
                if (iError != null) {
                    iError.onError(code, error);
                }
                if (iPageLoadListener != null) {
                    iPageLoadListener.hideLoading();
                }
            }
        });
    }

    /**
     *
     */
    public void delete(String url,
                       WeakHashMap<String, Object> params,
                       final ISuccess listener) {
        if (getRetrofit() == null) {
            return;
        }
        if (iPageLoadListener != null) {
            iPageLoadListener.showLoading();
        }
        RxService service = getRetrofit().create(RxService.class);
        setSubscribe(service.delete(url, params), new Observer<BaseEntries<Object>>() {
            @Override
            public void onNext(BaseEntries<Object> entries) {
                if (entries != null) {
                    if (entries.isOk()) {
                        if (listener != null) {
                            listener.onSuccess(GsonUtils.toJson(entries.getData()));
                        }
                    } else {
                        if (iError != null) {
                            iError.onError(entries.getCode(), entries.getMessage());
                        }
                    }
                }
                if (iPageLoadListener != null) {
                    iPageLoadListener.hideLoading();
                }
            }

            @Override
            public void onError(int code, String error) {
                if (iError != null) {
                    iError.onError(code, error);
                }
                if (iPageLoadListener != null) {
                    iPageLoadListener.hideLoading();
                }
            }
        });
    }


    /**
     *
     */
    public void upload(WeakHashMap<String, Object> lang,
                       String url,
                       String fileKey,
                       String filePath,
                       final ISuccess listener) {
        if (getRetrofit() == null) {
            return;
        }
        if (iPageLoadListener != null) {
            iPageLoadListener.showLoading();
        }
        RxService service = getRetrofit().create(RxService.class);
        File file = new File(filePath);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData(fileKey, file.getName(),
                RequestBody.create(MediaType.parse("multipart/form-data"), file));
        setSubscribe(service.upload(lang, url, filePart), new Observer<BaseEntries<Object>>() {
            @Override
            public void onNext(BaseEntries<Object> entries) {
                if (entries != null) {
                    if (entries.isOk()) {
                        if (listener != null) {
                            listener.onSuccess(GsonUtils.toJson(entries.getData()));
                        }
                    } else {
                        if (iError != null) {
                            iError.onError(entries.getCode(), entries.getMessage());
                        }
                    }
                }
                if (iPageLoadListener != null) {
                    iPageLoadListener.hideLoading();
                }
            }

            @Override
            public void onError(int code, String error) {
                if (iError != null) {
                    iError.onError(code, error);
                }
                if (iPageLoadListener != null) {
                    iPageLoadListener.hideLoading();
                }
            }
        });
    }

    /**
     *
     */
    public void upload(WeakHashMap<String, Object> lang,
                       String url,
                       String fileKey,
                       List<String> filePaths,
                       final ISuccess listener) {
        if (getRetrofit() == null) {
            return;
        }
        if (iPageLoadListener != null) {
            iPageLoadListener.showLoading();
        }
        RxService service = getRetrofit().create(RxService.class);
        List<MultipartBody.Part> parts = new ArrayList<>(filePaths.size());
        for (String filePath : filePaths) {
            File file = new File(filePath);
            MultipartBody.Part part = MultipartBody.Part.createFormData(fileKey, file.getName(),
                    RequestBody.create(MediaType.parse("multipart/form-data"), file));
            parts.add(part);
        }
        setSubscribe(service.upload(lang, url, parts), new Observer<BaseEntries<Object>>() {
            @Override
            public void onNext(BaseEntries<Object> entries) {
                if (entries != null) {
                    if (entries.isOk()) {
                        if (listener != null) {
                            listener.onSuccess(GsonUtils.toJson(entries.getData()));
                        }
                    } else {
                        if (iError != null) {
                            iError.onError(entries.getCode(), entries.getMessage());
                        }
                    }
                }
                if (iPageLoadListener != null) {
                    iPageLoadListener.hideLoading();
                }
            }

            @Override
            public void onError(int code, String error) {
                if (iError != null) {
                    iError.onError(code, error);
                }
                if (iPageLoadListener != null) {
                    iPageLoadListener.hideLoading();
                }
            }
        });
    }

    @Override
    protected ArrayList<Interceptor> interceptors() {
        return super.interceptors();
    }

    @NonNull
    protected OkHttpClient.Builder getOkHttpClient() {
        return super.getOkHttpClient();
    }


}
